package com.yyz.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyz.lease.common.constant.RedisConstant;
import com.yyz.lease.model.entity.*;
import com.yyz.lease.model.enums.ItemType;
import com.yyz.lease.web.admin.mapper.*;
import com.yyz.lease.web.admin.service.*;
import com.yyz.lease.web.admin.vo.attr.AttrValueVo;
import com.yyz.lease.web.admin.vo.graph.GraphVo;
import com.yyz.lease.web.admin.vo.room.RoomDetailVo;
import com.yyz.lease.web.admin.vo.room.RoomItemVo;
import com.yyz.lease.web.admin.vo.room.RoomQueryVo;
import com.yyz.lease.web.admin.vo.room.RoomSubmitVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 */
@Service
@RequiredArgsConstructor
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    private final GraphInfoService graphInfoService;
    private final RoomAttrValueService roomAttrValueService;
    private final RoomLabelService roomLabelService;
    private final RoomFacilityService roomFacilityService;
    private final RoomPaymentTypeService roomPaymentTypeService;
    private final RoomLeaseTermService roomLeaseTermService;

    private final RoomInfoMapper roomInfoMapper;
    private final ApartmentInfoMapper apartmentInfoMapper;
    private final GraphInfoMapper graphInfoMapper;
    private final AttrValueMapper attrValueMapper;
    private final LabelInfoMapper labelInfoMapper;
    private final FacilityInfoMapper facilityInfoMapper;
    private final PaymentTypeMapper paymentTypeMapper;
    private final LeaseTermMapper leaseTermMapper;

    private final RedisTemplate<String, Object> redisTemplate;

    /**
     * 分页查询
     */
    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page, queryVo);
    }

    /**
     * 根据id返回房间信息
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        //查询房间信息
        RoomInfo roomInfo = this.getById(id);

        //所属公寓信息
        LambdaQueryWrapper<ApartmentInfo> apartmentInfoQueryWrapper = new LambdaQueryWrapper<>();
        apartmentInfoQueryWrapper.eq(ApartmentInfo::getId, roomInfo.getApartmentId());
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectOne(apartmentInfoQueryWrapper);

        //房间属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.getAttrValueVosByRoomId(id);

        //房间标签
        List<LabelInfo> labelInfoList = labelInfoMapper.getLabelInfoByRoomId(id);

        //房间配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfoByRoomId(id);

        //房间图片
        List<GraphVo> graphVoList = graphInfoMapper.getGraphInfoByItemTypeAndItemId(ItemType.ROOM, id);

        //支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.getPaymentTypesByRoomId(id);

        //可选租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.getLeaseTermsByRoomId(id);

        //拼接信息
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }

    /**
     * 保存或更新房间信息
     */
    @Override
    @Transactional
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        //判断是否为修改
        boolean isUpdate = roomSubmitVo.getId() != null;
        saveOrUpdate(roomSubmitVo);

        if (isUpdate) {
            //修改操作
            //删除房间属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueQueryWrapper);

            //删除房间配套
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

            //删除房间标签
            LambdaQueryWrapper<RoomLabel> labelInfoQueryWrapper = new LambdaQueryWrapper<>();
            labelInfoQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(labelInfoQueryWrapper);

            //删除支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

            //删除可选租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

            //删除图片
            LambdaQueryWrapper<GraphInfo> graphInfoQuery = new LambdaQueryWrapper<>();
            graphInfoQuery.eq(GraphInfo::getItemType, ItemType.ROOM)
                    .eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoService.remove(graphInfoQuery);
        }

        //插入房间属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (attrValueIds != null) {
            List<RoomAttrValue> roomAttrValues = attrValueIds.stream()
                    .map(id -> RoomAttrValue.builder()
                            .roomId(roomSubmitVo.getId())
                            .attrValueId(id).build()).toList();
            roomAttrValueService.saveBatch(roomAttrValues);
        }

        //插入房间配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (facilityInfoIds != null) {
            List<RoomFacility> roomAttrValues = facilityInfoIds.stream()
                    .map(id -> RoomFacility.builder()
                            .roomId(roomSubmitVo.getId())
                            .facilityId(id).build()).toList();
            roomFacilityService.saveBatch(roomAttrValues);
        }

        //插入房间标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (labelInfoIds != null) {
            List<RoomLabel> roomLabels = labelInfoIds.stream()
                    .map(id -> RoomLabel.builder()
                            .roomId(roomSubmitVo.getId())
                            .labelId(id).build()).toList();
            roomLabelService.saveBatch(roomLabels);
        }

        //插入支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (paymentTypeIds != null) {
            List<RoomPaymentType> roomPaymentTypes = paymentTypeIds.stream()
                    .map(id -> RoomPaymentType.builder()
                            .roomId(roomSubmitVo.getId())
                            .paymentTypeId(id).build()).toList();
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }

        //插入可选租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (leaseTermIds != null) {
            List<RoomLeaseTerm> roomLeaseTerms = leaseTermIds.stream()
                    .map(id -> RoomLeaseTerm.builder()
                            .roomId(roomSubmitVo.getId())
                            .leaseTermId(id).build()).toList();
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }

        //插入图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (graphVoList != null) {
            List<GraphInfo> graphInfos = graphVoList.stream()
                    .map(graphVo -> {
                        GraphInfo graphInfo = new GraphInfo();
                        graphInfo.setName(graphVo.getName());
                        graphInfo.setUrl(graphVo.getUrl());
                        graphInfo.setItemType(ItemType.ROOM);
                        graphInfo.setItemId(roomSubmitVo.getId());
                        return graphInfo;
                    }).toList();
            graphInfoService.saveBatch(graphInfos);
        }

        //删除缓存
        redisTemplate.delete(RedisConstant.APP_ROOM_PREFIX + roomSubmitVo.getId());

    }

    /**
     * 删除房间信息
     */
    @Override
    @Transactional
    public void removeByRoomId(Long id) {
        //删除房间信息
        removeById(id);

        //删除房间属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueQueryWrapper);

        //删除房间配套
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

        //删除房间标签
        LambdaQueryWrapper<RoomLabel> labelInfoQueryWrapper = new LambdaQueryWrapper<>();
        labelInfoQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(labelInfoQueryWrapper);

        //删除支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        //删除可选租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

        //删除图片
        LambdaQueryWrapper<GraphInfo> graphInfoQuery = new LambdaQueryWrapper<>();
        graphInfoQuery.eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphInfoQuery);

        //删除缓存
        redisTemplate.delete(RedisConstant.APP_ROOM_PREFIX + id);
    }
}




